package com.jarveis.dbs.shfc.common;

import java.util.List;

import org.apache.log4j.Logger;

import com.jarveis.dbs.core.Service;
import com.jarveis.dbs.shfc.common.bean.SmsBean;
import com.jarveis.frame.jdbc.JdbcUtil;
import com.jarveis.frame.jdbc.handler.BeanListHandler;
import com.jarveis.frame.lang.StringUtil;
import com.jarveis.frame.util.Param;

/**
 * 校验短信验证码
 * 
 * @author liuguojun
 */
public class CheckSmsService implements Service {

	private static final Logger logger = Logger
			.getLogger(CheckSmsService.class);

	public Param callService(Param in) {
		Param out = null;

		try {
			out = new Param(Param.RESP);
			String phone = in.getBody().getString("@phone"); // 接收人的手机号码
			String code = in.getBody().getString("@code"); // 接收的验证码

			if (StringUtil.isEmpty(phone)) {
				out.getHead().setProperty(Param.LABEL_ERROR, 2002);
				return out;
			}

			if (StringUtil.isEmpty(code)) {
				out.getHead().setProperty(Param.LABEL_ERROR, 2003);
				return out;
			}

			// 查出最近5分钟的数据
			String sql = "select * from C_SMS where sms_to = :phone and is_succ = 1 and create_date > sysdate-(90/24/60) order by create_date desc";
			List data = (List) JdbcUtil.query(sql, new BeanListHandler(
					SmsBean.class), in.getBody().getPropertys());

			if (!data.isEmpty()) {
				SmsBean smsBean = (SmsBean) data.get(0);
				if (code.equals(smsBean.getSmsCode())) {
					out.getHead().setProperty(Param.LABEL_ERROR,
							Param.ERROR_SUCCESS);

					return out;
				}
			}

			out.getHead().setProperty(Param.LABEL_ERROR, 2001);
		} catch (Exception ex) {
			out.getHead().setProperty(Param.LABEL_ERROR, Param.ERROR_EXCEPTION);
			logger.error(ex.getMessage(), ex);
		}

		return out;
	}

}
